/*
 * @Author: Qimu
 * @Date: 2020-04-25 05:46:51
 * @LastEditors: Qimu
 * @LastEditTime: 2020-06-25 15:02:55
 * @Description: file content
 * @FilePath: \vue-admin-koa2\koa2_api\module\middleware\upLoad.js
 */
const multer = require('koa-multer')
//上传头像图片接口配置
let storage = multer.diskStorage({
  // 文件保存路径, 需要到public下面新建upload文件夹。
  destination: function (req, file, cb) {
    cb(null, 'public/admin/avatars')
  },
  //修改文件名称
  filename: function (req, file, cb) {
    let fileFormat = (file.originalname).split(".");
    cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
  }
})

//加载配置
const upload = multer({
  storage,
  limits: {
    fileSize: 1024 * 1024 / 2  //限制512KB
  }
});

module.exports = upload
